<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/include/tagLib.jsp" %>
<!doctype html>
<html lang="en">
<head>
    <title>${sieName}</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <%@include file="/include/cssLib.jsp" %>
</head>

<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <!-- 菜单 -->
            <tag:nav key="sysAdmin"/>

            <div class="tab-content">
                <div class="tab-pane active">
                    <div class="card col-md-10 col-md-offset-1">
                        <tag:sys-nav key="userList"/>
                        <div class="card-content">
                            <div class="tab-content">
                                <div class="tab-pane active">
                                    <div class="material-datatables table-responsive">
                                        <table id="datatables" class="table table-bordered table-hover" cellspacing="0" width="100%" style="width:100%">
                                            <thead>
                                            <tr>
                                                <th>姓名</th>
                                                <th>角色</th>
                                                <th>邮箱</th>
                                                <th>电话</th>
                                                <th>职位</th>
                                                <th>最后登陆ip</th>
                                                <th>最后登陆时间</th>
                                                <th>备注</th>
                                                <th>状态</th>
                                                <th>创建时间</th>
                                                <c:if test="${fns:isAdmin()}">
                                                    <th class="disabled-sorting text-center">操作</th>
                                                </c:if>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <c:forEach items="${pageResult.result }" var="data">
                                                <tr>
                                                    <td>${data.name}</td>
                                                    <td>
                                                        <c:if test="${data.role eq 0}">普通</c:if>
                                                        <c:if test="${data.role eq 1}">管理员</c:if>
                                                        <c:if test="${data.role eq 2}">超级管理员</c:if>
                                                    </td>
                                                    <td>${data.email}</td>
                                                    <td>${data.phone}</td>
                                                    <td>${data.position}</td>
                                                    <td>${data.loginIp}</td>
                                                    <td>${fns:formatDateTime(data.loginTime)}</td>
                                                    <td>${data.remarks}</td>
                                                    <td>
                                                        <c:if test="${data.status eq 0}"><label class='label label-danger'>禁用</label></c:if>
                                                        <c:if test="${data.status eq 1}"><label class='label label-success'>正常</label></c:if>
                                                        <c:if test="${data.status eq 2}"><label class='label label-gray'>离职</label></c:if>
                                                    </td>
                                                    <td>${fns:formatDateTime(data.createTime)}</td>
                                                    <c:if test="${fns:isAdmin()}">
                                                        <td>
                                                            <a href="javascript:void(0);" onclick="updateUserStatus(${data.id});">设置状态</a>
                                                        </td>
                                                    </c:if>
                                                </tr>
                                            </c:forEach>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<%@include file="/include/sidebarRight.jsp" %>
</body>
<%@include file="/include/scriptLib.jsp" %>
<script type="text/javascript" src="${ctxStatic }/jquery-plugs/jquery.datatables.js"></script>

<script type="text/javascript">
    $(document).ready(function() {
        //http://www.datatables.club/reference/option/
        $('#datatables').DataTable({
            "pagingType": "full_numbers",
            "lengthMenu": [
                [10, 25, 50, -1],
                [10, 25, 50, "All"]
            ],
            responsive: true,
            columnDefs: [
                {"targets": 2, "orderable": false},
                {"targets": 7, "orderable": false}
            ],
            language: {
                sSearch: "",
                searchPlaceholder: "搜索关键字",
            },

        });
        $('.card .material-datatables label').addClass('form-group');
    });

    function updateUserStatus(userId){

        swal({
            title: '更改状态',
            html:
            '<select id="newStatus" class="swal2-select form-control"><option value="">选择状态</option><option value="0">禁用</option><option value="1">正常</option><option value="2">离职</option></select>' +
            '<textarea id="reason" class="swal2-textarea" placeholder="填写更改理由"></textarea>',
            preConfirm: function () {
                return new Promise(function (resolve, reject) {
                    var newStatus = $('#newStatus').val();
                    var reason = $('#reason').val();
                    if (newStatus === "") {
                        reject("您还未选择状态");
                    }else if(reason === ""){
                        $('#reason').focus();
                        reject("请填写理由");
                    }else{
                        resolve([newStatus, reason])
                    }
                })
            }
        }).then(function (result) {
            HttpUtil.ajaxAsyncJsonPost("/adminConsole/user/setStatus", {userId: userId, status: result[0], reason: result[1]}, function(message){
                dialogTip(message);
                refreshPage(1200);
            });
        }).catch(swal.noop)
    }
</script>

</html>